import { defineStore } from "pinia";
import { useRouter } from 'vue-router';

export const userStore = defineStore("user", {
  state: () => ({
    isLoggedIn: false,
    user : null,
  }),
  actions: {
    login(userData) {
      this.isLoggedIn = true;
      this.user = userData;
      localStorage.setItem('authToken', 'simulated-token');
    },
    logout() {
        this.isLoggedIn = false;
        this.user = null;
        localStorage.removeItem('authToken');
        window.location.href = '/login';
    },
    initialize() {
      const token = localStorage.getItem('authToken');
      if (token) {
        this.isLoggedIn = true;
      }
    }
},  
   persist:{
    enabled: true,
    strategies: [
      {
        key: 'user',
        storage: localStorage
      }
    ]
   }, 
})